<?php
namespace App\Services\Api;

class KuaiDi100
{
    protected $customer = '2A6097F5F51CFC2240E5F05AC12AD104'; //公司编码

    protected $key = 'nmdqjWmk2159'; //KYY 值

    protected $query_url = 'http://poll.kuaidi100.com/poll/query.do';

    /**
     * @param $data[] company_code  快递公司编码
     * @param $data[] waybill_number    物流单号
     */
    public function getLogisticsInfo($data)
    {
        if(!$data['company_code'] || !$data['waybill_number']){
            return false;
        }
        //参数设置
        $post_data = array();
        $post_data["customer"] = $this->customer;
        $post_data["param"] = '{"com":"' . $data['company_code'] . '","num":"' . $data['waybill_number'] . '"}';

        $post_data["sign"] = md5($post_data["param"] . $this->key . $this->customer);
        $post_data["sign"] = strtoupper($post_data["sign"]);
        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $post_data = substr($o, 0, -1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $this->query_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        $data = str_replace("\&quot;", '"', $result);
        $data = json_decode($data, true);

        return $data;
    }
}